﻿using AutoMapper;
using Core.SimpleTemp.Application;
using Core.SimpleTemp.Application.UserApp;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Core.SimpleTemp.Repositories.Internal;


namespace Core.SimpleTemp.Api.Api.CBS
{
    /// <summary>
    /// CBS_BreakRules
    /// </summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class CBS_BreakRulesController : CoreApiController
    {
        private readonly CBS_BreakRulesService _service;
        private readonly CBS_BreakRulesApproveService _serviceApprove;
        private readonly SysUserAppService _userService;
        private readonly CoreDBContext _dbContext;

        /// <summary>
        /// CBS_BreakRules
        /// </summary>
        /// <param name="service"></param>
        /// <param name="userService"></param>
        /// <param name="serviceApprove"></param>
        /// <param name="coreDBContext"></param> 
        public CBS_BreakRulesController(CBS_BreakRulesService service, SysUserAppService userService, CBS_BreakRulesApproveService serviceApprove, CoreDBContext coreDBContext)
        {
            _service = service;
            _userService = userService;
            _serviceApprove = serviceApprove;
            _dbContext = coreDBContext;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetList")]
        [PermissionFilter(nameof(CBS_BreakRulesController) + "_" + nameof(CBS_BreakRulesController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            GetList(PagingModel<CBS_BreakRulesDto>
                pagingQueryModel)
        {
            IPageModel<CBS_BreakRulesDto>
                result;
            result = await _service.GetList(pagingQueryModel);
            return this.JsonSuccess(result);
        }

        /// <summary>
        /// 查询该用户需要审批的列表
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>
        [HttpPost("GetApproveList")]
        [PermissionFilter(nameof(CBS_BreakRulesController) + "_" + nameof(CBS_BreakRulesController.GetApproveList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            GetApproveList(PagingModel<CBS_BreakRulesDto>
                pagingQueryModel)
        {
            IPageModel<CBS_BreakRulesDto>
                result;
            result = await _service.GetApproveList(pagingQueryModel);
            return this.JsonSuccess(result);
        }


        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("Save")]
        [PermissionFilter(nameof(CBS_BreakRulesController) + "_" + nameof(CBS_BreakRulesController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(CBS_BreakRulesDto dto)
        {

            for(int i=0;i< dto.CBS_BreakRulesApprove.Count;i++)
            {
                if (dto.CBS_BreakRulesApprove[i].ExaminUserType == "01")
                {
                    dto.CBS_BreakRulesApprove[i].ExamineUser = UserIdentity.Id;
                }
            }
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                dto.CreateFactory = UserIdentity.Factory;
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }


        /// <summary>
        /// 修改审批状态
        ///</summary>
        /// <param name="dto"></param>
        /// <param name="dto1"></param>
        /// <returns></returns>
        [HttpPost("SaveApprove")]
        [PermissionFilter(nameof(CBS_BreakRulesController) + "_" + nameof(CBS_BreakRulesController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveApproveAsync(CBS_BreakRulesDto dto)
        {

            if (dto.State == "6")  //领导批准
            {
                //修改承包商人员状态
                var newEntity = Mapper.Map<List<CBS_User>>(dto.ViolatorsID);
                newEntity.ForEach(e =>
                {
                    e.IsBack = true;
                });
                await _dbContext.AddRangeAsync(newEntity);
                await _dbContext.SaveChangesAsync();
            }
            await _service.UpdateAsync(dto);
            return this.JsonSuccess(string.Empty);
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(CBS_BreakRulesController) + "_" + nameof(CBS_BreakRulesController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult>
            DeleteMutiAsync([FromBody]Guid[] ids)
        {

            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }



        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult>
            Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            CBS_BreakRulesDto CBS_BreakRulesDto = Mapper.Map<CBS_BreakRulesDto>(model);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            //当前登陆用户信息
            var user = await _userService.GetAsync(UserIdentity.Id);
            var result = new {
                SysUser = new
                {
                    id = user.Id,
                    name = user.SysUserInfor.Name
                },
                cbs_breakrules = CBS_BreakRulesDto
            };
            return this.JsonSuccess(result);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="id2"></param>
        /// <returns></returns>
        [HttpGet("Get2")]
        public async Task<IActionResult>
          Get2(Guid id,Guid id2)
        {
            var model = await _service.GetAsync(id);
            CBS_BreakRulesDto CBS_BreakRulesDto = Mapper.Map<CBS_BreakRulesDto>(model);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            //当前登陆用户信息
            var user = await _userService.GetAsync(UserIdentity.Id);


            var model2 = await _serviceApprove.GetAsync(id2);
            CBS_BreakRulesApproveDto CBS_BreakRulesApproveDto = Mapper.Map<CBS_BreakRulesApproveDto>(model2);


            var result = new
            {
                SysUser = new
                {
                    id = user.Id,
                    name = user.SysUserInfor.Name
                },
                cbs_breakrules = CBS_BreakRulesDto,
                cbs_breakRulesApprove = CBS_BreakRulesApproveDto
            };
            return this.JsonSuccess(result);
        }
    
        /// <summary>
        /// 导出
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("Export")]
        [PermissionFilter(nameof(CBS_BreakRulesController) + "_" + nameof(CBS_BreakRulesController.Export), AuthorizationConst.EXPORT)]
        public async Task<IActionResult> Export(PagingModel<CBS_BreakRulesDto> pagingQueryModel)
        {
            var fileName = await _service.Export(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }

        #endregion
    }
}
